﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Xml;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class PrintReport4AXBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICycleCountCriteriaDA cycleCountCriteriaDA = DataAccess.CreateCycleCountCriteriaDA();
        private List<string> dataBaseID;
        private InboundHelper inboundHelper;
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private static readonly IPrintReport4AXDA printReport4AXDA = DataAccess.CreatePrintReport4AXDA();
        private PrintReportsInfo printReportsInfo;
        private static readonly IPhysicalRequestDA RequestDA = DataAccess.CreatePhysicalRequestDA();
        private static readonly IPhysicalTagDA TagDA = DataAccess.CreatePhysicalTagDA();

        public PrintReport4AXBLL()
        {
            this.inboundHelper = new InboundHelper();
            this.printReportsInfo = null;
        }

        public PrintReport4AXBLL(string currentWH)
        {
            this.inboundHelper = new InboundHelper();
            this.printReportsInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PrintReport4AXBLL(PrintReportsInfo tempPrintReportsInfo)
        {
            this.inboundHelper = new InboundHelper();
            this.printReportsInfo = null;
            if (!string.IsNullOrEmpty(tempPrintReportsInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPrintReportsInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", tempPrintReportsInfo.UserLanguage);
                }
                else
                {
                    this.printReportsInfo = new PrintReportsInfo();
                    this.printReportsInfo = tempPrintReportsInfo;
                    this.printReportsInfo.WhLoignID = this.dataBaseID[0];
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", tempPrintReportsInfo.UserLanguage);
            }
        }

        protected string DataSet2XML(DataSet ds)
        {
            int i;
            XmlDocument xml = new XmlDocument();
            xml.AppendChild(xml.CreateNode(XmlNodeType.XmlDeclaration, "", ""));
            XmlElement root = xml.CreateElement("DataSet");
            xml.AppendChild(root);
            XmlElement fieldNodes = xml.CreateElement("Fields");
            root.AppendChild(fieldNodes);
            for (i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                XmlElement field = xml.CreateElement(string.Format("Field{0}", i + 1));
                field.SetAttribute("FieldName", ds.Tables[0].Columns[i].ColumnName.Trim().ToUpper());
                field.SetAttribute("FieldType", ds.Tables[0].Columns[i].DataType.ToString().Trim());
                fieldNodes.AppendChild(field);
            }
            XmlElement rowNodes = xml.CreateElement("Rows");
            root.AppendChild(rowNodes);
            for (i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                XmlElement row = xml.CreateElement(string.Format("Row{0}", i + 1));
                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                    row.SetAttribute(ds.Tables[0].Columns[j].ColumnName.Trim().ToUpper(), ds.Tables[0].Rows[i][j].ToString().Trim());
                }
                rowNodes.AppendChild(row);
            }
            return xml.OuterXml;
        }

        public string GetPrintExpressDelivery(string orderID)
        {
            DataSet ds = printReport4AXDA.GetPrintExpressDelivery(orderID, this.printReportsInfo);
            return this.DataSet2XML(ds);
        }

        public string GetPrintHandoverNote(string orderID)
        {
            DataSet ds = printReport4AXDA.GetPrintHandoverNote(orderID, this.printReportsInfo);
            return this.DataSet2XML(ds);
        }

        public string GetPrintShippingOrderList(string orderID)
        {
            DataSet ds = printReport4AXDA.GetPrintShippingOrderList(orderID, this.printReportsInfo);
            return this.DataSet2XML(ds);
        }
    }
}

